﻿using furion.blog.Domain.Shared;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.Domain.Query.RequestLog
{
    public class RequestLogListQuery : BaseQuery
    {
        public string Date { get; set; } = string.Empty;
        public string Role { get; set; } = string.Empty;
        public string SearchContent { get; set; } = string.Empty;
        public string RequestMethod { get; set; } = string.Empty;

        private string Columns = " t.log_id as logId,t.request_method as requestMethod,t.ip,t.source_addr as sourceAddr," +
                "t.os_info as osInfo,t.execute_method as executeMethod,t.api_name as apiName,t.api_desc as apiDesc,t.url," +
                "t.body,t.role,t.uid,t.result,t.used_time as usedTime,t.created_time as createdTime ";

        private string TableName() {
            return "request_log_" + Date;
        }

        public string GetCountSQL()
        {
            var (strWhere, sqlParams) = QueryParams();
            return " select count(t.log_id) from `" + TableName() + "` t where 1=1 "+strWhere;
        }

        public string GetDataSQL() {
            var (strWhere, sqlParams) = QueryParams();
            return " select " + Columns + " from `" + TableName() + "` t " +
                " where t.created_time<=(select t.created_time from `" + TableName() + "` t where 1=1 " + strWhere + " order by t.created_time desc limit " + GetStartIndex() + ",1 ) " +
                " " + strWhere + " order by t.created_time desc limit " + PageSize;
        }

        /// <summary>
        /// 返回查询where 和 条件参数
        /// </summary>
        /// <returns></returns>
        public (string, List<SugarParameter>) QueryParams()
        {
            List<SugarParameter> parameters = new List<SugarParameter>();
            StringBuilder strWhere = new StringBuilder();
            if (!string.IsNullOrWhiteSpace(SearchContent))
            {
                strWhere.AppendLine(" and (t.ip like @searchContent " +
                    " or t.source_addr like @searchContent " +
                    " or t.os_info like @searchContent " +
                    " or t.execute_method like @searchContent " +
                    " or t.api_name like @searchContent " +
                    " or t.api_desc like @searchContent " +
                    " or t.url like @searchContent " +
                    " or t.uid like @searchContent " +
                    " ) ");
                parameters.Add(new SugarParameter("searchContent", "%"+ SearchContent + "%"));
            }
            if (!string.IsNullOrWhiteSpace(Role))
            {
                strWhere.AppendLine(" and t.role=@role ");
                parameters.Add(new SugarParameter("role", Role));
            }
            if (!string.IsNullOrWhiteSpace(RequestMethod))
            {
                strWhere.AppendLine(" and t.request_method=@requestMethod ");
                parameters.Add(new SugarParameter("requestMethod", RequestMethod));
            }

            return (strWhere.ToString(), parameters);
        }

    }
}
